/**
 *Copyright 2013 HEBCA
 *All Right Reserved
 *Author ：gaobiao
 *编码日期格式：2013-4-17
 */

package com.school.common.tags;

import java.io.IOException;
import java.io.Writer;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.components.Component;
import org.apache.struts2.views.jsp.ComponentTagSupport;

import com.opensymphony.xwork2.util.ValueStack;

/**
 * @author gaobiao
 * 
 */
public class JustifyTag extends ComponentTagSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = 5113114233102056909L;
	private String value;
	private int maxlength;
	private String endStr;

	public String getEndStr() {
		return endStr;
	}

	public void setEndStr(String endStr) {
		this.endStr = endStr;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	public int getMaxlength() {
		return maxlength;
	}

	public void setMaxlength(int maxlength) {
		this.maxlength = maxlength;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.apache.struts2.views.jsp.ComponentTagSupport#getBean(com.opensymphony
	 * .xwork2.util.ValueStack, javax.servlet.http.HttpServletRequest,
	 * javax.servlet.http.HttpServletResponse)
	 */
	@Override
	public Component getBean(ValueStack valueStack, HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		return new JustifyComponent(valueStack);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.apache.struts2.views.jsp.ComponentTagSupport#populateParams()
	 */
	@Override
	protected void populateParams() {
		// TODO Auto-generated method stub
		super.populateParams();
		JustifyComponent justifyComponent = (JustifyComponent) component;
		justifyComponent.setValue(value);
		justifyComponent.setMaxlength(maxlength);
		justifyComponent.setEndStr(endStr);
	}

	class JustifyComponent extends Component {
		private String value;
		private int maxlength;
		private String endStr = "";

		public String getEndStr() {
			return endStr;
		}

		public void setEndStr(String endStr) {
			this.endStr = endStr;
		}

		public String getValue() {
			return value;
		}

		public void setValue(String value) {
			this.value = value;
		}

		public int getMaxlength() {
			return maxlength;
		}

		public void setMaxlength(int maxlength) {
			this.maxlength = maxlength;
		}

		/**
		 * @param stack
		 */
		public JustifyComponent(ValueStack stack) {
			super(stack);
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see org.apache.struts2.components.Component#end(java.io.Writer,
		 * java.lang.String)
		 */
		@Override
		public boolean end(Writer writer, String body) {
			boolean result = super.start(writer);
			value = String.valueOf(this.getStack().findValue(value));
			String html = "";
			if (value != null && value.length() > 0) {
				if (maxlength > 0 && len(value) > maxlength) {
					html = "<span title='" + value + "'>" + cut(value, maxlength - len(endStr)) + endStr + "</span>";
				} else {
					html = value;
				}
			}
			try {
				writer.write(html);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return result;
		}

		private int len(String s) {
			if (s == null)
				return 0;
			int ln = 0, i = 0;
			char c;
			for (i = 0; i < s.length(); i++) {
				c = s.charAt(i);
				if (c >= ' ' && c <= '~') // 所有单字节字符
					ln += 1;
				else
					ln += 2;
			}
			return ln;
		}

		private String cut(String s, int xlen) {
			if (s == null) {
				s = "";
			}
			int ln = 0, i = 0;
			String t = "";
			char c;
			for (i = 0; i < s.length(); i++) {
				c = s.charAt(i);
				if (c >= ' ' && c <= '~') //
					ln += 1;
				else
					ln += 2;
				if (ln == xlen) {
					i++;
					t = s.substring(0, i);
					break;
				} else if (ln > xlen) {
					t = s.substring(0, i) + " ";
					break;
				}
			}
			return t;
		}
	}
}
